package com.jf.springboot.web;

import com.jf.springboot.bean.Person;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-12-05
 * Time:11:20
 */
@RequestMapping("/params")
@RestController
public class ParamsController {
    @GetMapping("/person/{id}/{name}")
    public String person(@PathVariable(value = "id") int id, @PathVariable(value = "name") String name) {
        System.out.println("id: " + id + ", name: " + name);
        return id + " " + name;
    }
    @GetMapping("/student/{id}/{name}")
    public String student(@PathVariable Map<String, String> map) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("key: " + entry.getKey() + ", value: " + entry.getValue());
        }
        return map.toString();
    }
    @GetMapping("/header")
    public String header(@RequestHeader Map<String, String> map) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("key: " + entry.getKey() + ", value: " + entry.getValue());
        }
        return map.toString();
    }
    @GetMapping("/p")
    public String params(@RequestParam("id") Integer id,
            @RequestParam("friends") List<String> friends,
            @RequestParam Map<String, String> map) {
        System.out.println("id: " + id + ", friends: " + friends);
        System.out.println(map);
        return map.toString();
    }
    @GetMapping("/cookie")
    public String cookie(@CookieValue("username") String uname,
                         @CookieValue("c-key") Cookie cookie,
                         HttpServletRequest request) {
        System.out.println("uname: " + uname + ", c-key: " + cookie.getName() + "-" + cookie.getValue());
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie c : cookies) {
                System.out.println("cookie: " + c.getName() + "-" + c.getValue());
            }
        }
        return uname;
    }
    @PostMapping("/json")
    public String json(@RequestBody String json) {
        System.out.println(json);
        return "success";
    }
    @PostMapping("/save")
    public Person save(Person person) {
        System.out.println(person);
        return person;
    }
}
